package com.cgnpc.scp.common.controller;

import cn.com.cgnpc.aep.bizcenter.appcenter.sdk.result.ApiResult;
import com.cgnpc.scp.common.mybatis.entity.ScpCommonSmsInfo;
import com.cgnpc.scp.common.service.IScpCommonSmsInfoService;
import com.cgnpc.scp.common.service.SmsCenterService;
import com.cgnpc.scp.common.utils.ListUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/******************************
 * 用途说明: SCP短信组件
 * 作者姓名: yblrs
 * 创建时间: 2020/8/20 22:20
 ******************************/
@Component
public class ScpCommonSmsInfoController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScpCommonFileDownloadController.class);

    @Autowired
    private IScpCommonSmsInfoService scpCommonSmsInfoService;

    @Autowired
    private SmsCenterService smsCenterService;

    /**********************************
     * 用途说明: 调试HSE管理-会议信息---发送短信
     * 参数说明:
     * 返回值说明: Object
     ***********************************/
    // @Scheduled(cron = "0 */5 * * * ?") // 每5分钟执行一次
    // @Scheduled(cron = "0/5 * * * * ?") // 每5秒执行一次
    public void autoSendSms() {
        // 发送短信需要登录人信息，去掉自动发送

        try {
            final int pageSize = 1000;
            int pageNum = 1;
            boolean hasNext = true;

            while (hasNext) {
                List<ScpCommonSmsInfo> dbScpCommonSmsInfoList = scpCommonSmsInfoService.loadNeedSendSms(pageNum, pageSize);
                int dbScpCommonSmsInfoListSize = ListUtil.getListSizeWith0(dbScpCommonSmsInfoList);
                hasNext = dbScpCommonSmsInfoListSize >= pageSize;

                if (dbScpCommonSmsInfoListSize <= 0) {
                    return;
                }

                for (ScpCommonSmsInfo dbScpCommonSmsInfo : dbScpCommonSmsInfoList) {
                    ApiResult apiResult = smsCenterService.sendSmsToSmsCenter(dbScpCommonSmsInfo);
                    scpCommonSmsInfoService.updateSmsSend(dbScpCommonSmsInfo.getId(), apiResult);
                }
            }
        } catch (Exception e) {
            LOGGER.error("autoSendSms.error;", e);
        }
    }

}
